package cn.cjxy.controller;

import cn.cjxy.domain.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 拦截器
 */

public class MyInterceptor implements HandlerInterceptor {
    /**
     * 请求执行前
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //返回值ture和false代表通过和不通过
        //检查session中是否有用户信息
        HttpSession session = request.getSession();
        User loginUser = (User) session.getAttribute("loginUser");
        //判断loginUser是否为空
        if (loginUser == null) {
            //没登陆
            //回到登录页
            session.setAttribute("error","您尚未登录，请登陆后再试！");
            //跳转回登陆页面
            response.sendRedirect("/login.jsp");
            return false;
        }
        //通过
        return true;
    }
    //请求执行
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }
}
